package com.venutech.gongan.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.venutech.gongan.common.Convert;
import com.venutech.gongan.dao.UserGroupRoleMapper;
import com.venutech.gongan.entity.UserGroupRole;
import com.venutech.gongan.service.UserGroupRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserGroupRoleServiceImpl implements UserGroupRoleService {
    @Resource
    private UserGroupRoleMapper userGroupRoleMapper;

    Logger logger = LoggerFactory.getLogger(UserGroupRoleServiceImpl.class);


    @Override
    public int insert(UserGroupRole userGroupRole) {
        return userGroupRoleMapper.insert(userGroupRole);
    }

    @Override
    public int delete(Integer ugId, Integer roleId) {
        logger.info("用户组删除角色 UserGroupRoleServiceImpl：=========="+ugId+":"+roleId);
        return userGroupRoleMapper.delete(ugId,roleId);
    }

    @Override
    public Page<UserGroupRole> getInfo(Integer pageNo, Integer pageSize, Integer ug_id) {
        Page<UserGroupRole> page = new Page<>(pageNo,pageSize);
        List<UserGroupRole> list  =  userGroupRoleMapper.getList(page,ug_id);
        return page.setRecords(list);
    }

    @Override
    public UserGroupRole searchUserGroupUser(Integer ugId, Long roleId) {
        return userGroupRoleMapper.searchUserGroupRole(ugId,roleId);
    }

    @Override
    public int UserGroupAddRoles(String ids, Integer ug_id) {
        Long[] idss = Convert.toLongArray(ids);

        //将数组转换集合
        List<Long> list = new ArrayList<>();
        for (Long uid:idss) {
            list.add(uid);
        }
        logger.info("UserGroupRoleServiceImpl List<Long> list 批量添加角色===========："+list);
        List<UserGroupRole> userGroupRoleList = new ArrayList<>();
        UserGroupRole user=null;
        for (int i = 0; i <list.size() ; i++) {
            user = new UserGroupRole();
            user.setRole_id(list.get(i));
            user.setUsergroup_id(ug_id);
            userGroupRoleList.add(user);
        }
        logger.info("UserGroupRoleServiceImpl List 批量添加角色===========："+userGroupRoleList);
        //判断角色
        for (int j = 0; j <list.size() ; j++) {
            UserGroupRole userGroupRole = userGroupRoleMapper.searchUserGroupRole(ug_id,list.get(j));
            if (userGroupRole!=null){
                //存在
                return -1;
            }
            break;
        }

        return userGroupRoleMapper.UserGroupAddRoles(userGroupRoleList);
    }

    @Override
    public List<Integer> getUserGroupRoles(Integer userId) {
        List<Integer> roleIds = userGroupRoleMapper.getUserGroupRoles(userId);
        return roleIds;

    }
}
